package model;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;
import entity.Category;

public class CategoryDAO {

    /**
     * Load Category in Shop.
     * @return List Category
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public ArrayList<Category> getList() throws Exception {
        ArrayList<Category> list = new ArrayList<Category>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<Category>) session.createCriteria(Category.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New Category
     * @param Category
     * @throws Exception
     */
    public void add(Category category) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(category);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update Category
     * @param Category
     * @throws Exception
     */
    public static void update(Category category) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(category);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete Category
     * @param Category
     * @throws Exception
     */
    public void delete(Category category) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(category);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find Category by ID
     * @param id
     * @return Category
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public Category find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        Category category = new Category();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            category = (Category) session.get(Category.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return category;
        }
    }
}
